package labox.innovation.gameserver.network.clientpackets;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import labox.innovation.gameserver.model.L2Object;
import labox.innovation.gameserver.model.actor.instance.FuzeNpcInstance;
import labox.innovation.gameserver.model.actor.instance.FuzePcInstance;
import labox.innovation.gameserver.network.SystemMessageId;
import labox.innovation.gameserver.network.serverpackets.SystemMessage;

/**
 * 客户端请求交互对象
 *
 * @version $Revision: 1.7.4.4 $ $Date: 2005/03/27 18:46:19 $
 */
public final class Action extends L2GameClientPacket {

	private static final String ACTION__C__04 = "[C] 04 Action";

	private static final Logger _log = LoggerFactory.getLogger(Action.class.getName());

	private long _objectId;

	@Override
	protected void readImpl() {
		_objectId = readLong(); // Target object Identifier
	}

	@Override
	protected void runImpl() {
		if (_log.isDebugEnabled())
			_log.debug("oid:{}", _objectId);

		// Get the current L2PcInstance of the player
		final FuzePcInstance activeChar = getClient().getActiveChar();

		if (activeChar == null)
			return;

		final L2Object obj;
		
		if (activeChar.getTargetId() == _objectId) {
			obj = activeChar.getTarget();
		} else
			obj = activeChar.getKnownList().getKnownCharacters().get(_objectId);
		
		if (obj == null) {
			return;
		}
		
		// 玩家不能交互不在同一个副本中的对象，除了在多元交互副本中
		if (obj.getInstanceId() != activeChar.getInstanceId() && activeChar.getInstanceId() != -1) {
			return;
		}
		
		// 仅仅GM能交互不可见的对象
		if (obj instanceof FuzePcInstance && (((FuzePcInstance) obj).getAppearance().getInvisible()) && !activeChar.isGM()) {
			return;
		}
		
		//-- 若目标是NPC的话判断与当前玩家种族是否匹配，若不匹配则跳出
		if (obj instanceof FuzeNpcInstance) {
			if (!((FuzeNpcInstance)obj).isRaceMatch(activeChar)) {
				activeChar.sendPacket(new SystemMessage(SystemMessageId.NPC_RACE_NOT_MATCHED));  //-- 发送提示信息
				return;
			}
		}
		
		// 检查的目标是否有效的，如果玩家不是在商店或交互请求状态中（例如：加好友，加入家族，加入队伍 ...）
		
		if (activeChar.getActiveRequester() == null || obj instanceof FuzeNpcInstance) {
			obj.onAction(activeChar);
		}
	}

	/* (non-Javadoc)
	 * @see labox.innovation.gameserver.clientpackets.ClientBasePacket#getType()
	 */
	@Override
	public String getType() {
		return ACTION__C__04;
	}
}
